package com.adms.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.adms.service.ImageService;
import com.hazelcast.util.Base64;

@RequestMapping("image")
@RestController
public class ImageController {
	
	@Autowired
	private ImageService imageService;
	
	@GetMapping("getCutImage")
	public void getCutImage(@RequestParam("imageId") Integer imageId, Integer width,Integer height,HttpServletRequest request,HttpServletResponse response) throws IOException {
		imageService.getCutImage(imageId,width,height,request,response);
	}
	
	@GetMapping(value="/{imageId}/{width}/{height}")
	public void getImg(@PathVariable("imageId") Integer imageId,@PathVariable("width") Integer width,@PathVariable("height") Integer height,HttpServletRequest request,HttpServletResponse response) throws Exception{
		imageService.getImg(imageId,width,height,request,response);
	}
	@GetMapping(value="/{imageId}/{width}")
	public void getImg(@PathVariable("imageId") Integer imageId,@PathVariable("width") Integer width,HttpServletRequest request,HttpServletResponse response) throws Exception{
		imageService.getImg(imageId,width,null,request,response);
	}
	@GetMapping(value="/{imageId}")
	public void getImg(@PathVariable("imageId") Integer imageId,HttpServletRequest request,HttpServletResponse response) throws Exception{
		imageService.getImg(imageId,null,null,request,response);
	}

	@PostMapping("upload")
	public Object uploadImg(@RequestParam("files") MultipartFile[] files) {
		return imageService.uploadImg(files);
	}
	
	@PostMapping("delete")
	public Object delete(@RequestParam("imageIds") int[] imageIds) {
		return imageService.delete(imageIds);
	}
	
	@PostMapping("upload2")
	public  String uploadImg2(HttpServletRequest request) throws Exception {
		// new一个文件对象用来保存图片,默认保存当前工程根目录
		File imageFile = new File("D:/abc.jpg");
		String parameter = request.getParameter("file");
		String parameter2 = request.getParameter("base64");
		String resultStr = request.getParameter("base64").toString();// 前端传来的是压缩后的并用base64编码后的字符串
		resultStr = resultStr.substring(resultStr.indexOf(",") + 1);// 需要去掉头部信息,这很重要
		System.out.println(resultStr);
		// 创建输出流
		FileOutputStream outputStream = new FileOutputStream(imageFile);
		// 获得一个图片文件流,我这里是从flex中传过来的
		byte[] result = Base64.decode(resultStr.getBytes());//解码
		for (int i = 0; i < result.length; ++i) {
			if (result[i] < 0) {// 调整异常数据
				result[i] += 256;
			}
		}
		outputStream.write(result);
		outputStream.flush();
		outputStream.close();
		return "sc";
	}

}
